/*
 * Copyright 2008 Hector Francisco Hernandez
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

var formShootCallback;

function formInit(canvas)
{
  formHTML = "";

  formHTML += "<form name=\"formName\" style=\"position:absolute; " +
      "left:0px; top:0px; width:640px; height:480px; color:orange; " +
      "text-align:left; visibility:hidden\">";
  formHTML += "<br />";
  formHTML += "&nbsp;&nbsp;Angle: <input type=\"text\" name=\"angle\" " +
      "size=\"3\" maxlength=\"3\">&nbsp;&nbsp;";
  formHTML += "<br />";
  formHTML += "&nbsp;&nbsp;Velocity: <input type=\"text\" name=\"velocity\" " +
      "size=\"3\" maxlength=\"3\">&nbsp;&nbsp;";
  formHTML += "<br />";
  formHTML += "&nbsp;&nbsp;<input type=\"button\" value=\"Shoot!\" " +
      "onclick=\"return formButton()\">&nbsp;&nbsp;";
  formHTML += "<br />";
  formHTML += "<span style=\"color:red\" id=\"formErrorMessage\"> </span>";
  formHTML += "</form>";

  canvas.innerHTML += formHTML;

}

function formButton()
{
  errors = new Array();

  angle = parseInt(document.formName.angle.value);
  if (isNaN(angle)) {
    errors.push("Invalid Angle!");
  }

  velocity = parseInt(document.formName.velocity.value);
  if (isNaN(velocity)) {
    errors.push("Invalid Velocity!");
  }

  errorMessage = document.getElementById("formErrorMessage");
  errorMessage.innerHTML = "";
  for (error in errors) {
    errorMessage.innerHTML += "&nbsp;&nbsp;" + errors[error] +
        "&nbsp;&nbsp;<br />";
  }

  if (errors.length > 0) {
    return;
  }

  document.formName.style.visibility = "hidden";

  formShootCallback(angle, velocity);

}

function formGetShoot(side, func)
{
  formShootCallback = func;
  document.formName.angle.value = "";
  document.formName.velocity.value = "";
  document.formName.style.visibility = "visible";
  document.formName.style.textAlign = side;
}

